package com.bear.cmcc.NetTestHTTP.service;

import java.util.ArrayList;

import android.app.Service;
import android.content.Intent;
import android.content.pm.ServiceInfo;
import android.os.IBinder;
import android.util.Log;

import com.bear.cmcc.NetTestHTTP.component.NetTestLogBean;
import com.bear.cmcc.NetTestHTTP.component.PowerLock;
import com.bear.cmcc.NetTestHTTP.constant.HttpTestDBConstant;
import com.bear.cmcc.NetTestHTTP.constant.NetTestDBConstant;
import com.bear.cmcc.NetTestHTTP.constant.MyCommonConstant.PrivateActionName;
import com.bear.cmcc.NetTestHTTP.db.HttpTestDBControl;
import com.bear.cmcc.NetTestHTTP.util.HttpTestUtils;
import com.bear.cmcc.NetTestHTTP.util.MyCommonUtil;

/**
 * 测试服务
 * 
 * @author zhangheng
 */
public class HttpTestService extends Service {
	static String TAG = HttpTestService.class.getSimpleName();

	ServiceInfo[] ourServices;// 其他service的集合

	ArrayList<String> stopedServicesName;// 暂停掉的其他service的名称集合

	private HttpTestThread threadTest;// 测试线程

	private PowerLock powerLock;

	int nextRoundID;
	
	NetTestLogBean netLogBean;

	HttpTestDBControl dbControl;

	@Override
	public void onCreate() {
		super.onCreate();
		Log.i(TAG, TAG + " onCreate.");
		try {
			if (powerLock == null)
				powerLock = new PowerLock(this, "NetTestService");
			powerLock.acquireWakeLock();
		} catch (Exception e) {
			e.printStackTrace();
		}

		stopedServicesName = new ArrayList<String>();
	}

	@Override
	public IBinder onBind(Intent intent) {
		Log.i(TAG, TAG + " onBind.");
		return null;
	}

	@Override
	public void onStart(Intent intent, int startId) {
		super.onStart(intent, startId);
		Log.i(TAG, TAG + " onStart.");
		try {
			if (powerLock == null)
				powerLock = new PowerLock(this, "HttpTestService");
			powerLock.acquireWakeLock();
		} catch (Exception e) {
			e.printStackTrace();
		}

		ourServices = MyCommonUtil.getAllService(getBaseContext());// 获取所有service的对象
		stopedServicesName = new ArrayList<String>();// 每次开启服务都实例化一次有暂停的服务名称集合
		dbControl = new HttpTestDBControl(getBaseContext());

		if (intent == null
				|| intent.getStringExtra(PrivateActionName.SERVICE_ACTION) == null)
			return;

		String action = intent.getStringExtra(PrivateActionName.SERVICE_ACTION);

		if (action
				.equalsIgnoreCase(PrivateActionName.SERVICE_ACTION_STARTSERVICE)) {// 开始测速

			stopedServicesName = HttpTestUtils.stopOtherService(getBaseContext(),
					ourServices, this.getClass());
			netLogBean = new NetTestLogBean();
			nextRoundID = dbControl
					.selectLastRowPid(HttpTestDBConstant.TABLES_TESTROUNDINFO_NAME) + 1;
			startTest();
		}
		if (action
				.equalsIgnoreCase(PrivateActionName.SERVICE_ACTION_STOPSERVICE)) {// 关闭测速
			if (threadTest != null && threadTest.isAlive()) {
				threadTest.interrupt();
			}
			if (powerLock != null)
				powerLock.releaseWakeLock();
			stopService();
			HttpTestUtils.startOtherService(getBaseContext(), stopedServicesName,
					ourServices);
		}

	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		Log.i(TAG, TAG + " onDestroy.");
		try {
			if (powerLock != null) {
				powerLock.releaseWakeLock();
			}
			powerLock = null;
		} catch (Exception e) {
			e.printStackTrace();
		}
		stopForeground(true);
	}

	/*
	 * 关闭服务
	 */
	private void stopService() {
		Log.i(TAG, "stopService isAutoTest ");
		stopSelf();
	}

	/*
	 * 开始测试
	 */
	private void startTest() {
		testThread();
	}

	/*
	 * 测试线程
	 */
	private void testThread() {
		threadTest = new HttpTestThread(getBaseContext());
		threadTest.start();
	}

}
